from django.utils.safestring import mark_safe
from django.urls import re_path, reverse
from django.http import QueryDict
from stark.service.s1 import StarkHandler
from app_main import models
from app_main.forms.for_stark import ConsultRecordModelForm


class ConsultRecordHandler(StarkHandler):  # 跟进记录
    list_template = 'app_main/consult_follow.html'
    model_form_class = ConsultRecordModelForm

    def display_edit(self, obj=None, is_head=None, *args, **kwargs):  # 自定义函数在页面中显示新的一列
        if is_head:
            return '编辑'

        cid = kwargs.get('cid')
        base_url = reverse('%s:%s' % (self.site.namespace, self.get_edit_url_name), kwargs={'cid': cid, 'nid': obj.id})
        if not self.request.GET:
            edit_url = base_url
        else:
            edit_url_dict = QueryDict(mutable=True)
            edit_url_dict['_addi'] = self.request.GET.urlencode()
            edit_url = '%s?%s' % (base_url, edit_url_dict.urlencode())

        return mark_safe("<a href='%s'>编辑</a>" % edit_url)

    def display_del(self, obj=None, is_head=None, *args, **kwargs):
        if is_head:
            return '删除'

        cid = kwargs.get('cid')
        base_url = reverse('%s:%s' % (self.site.namespace, self.get_delete_url_name), kwargs={'cid': cid, 'nid': obj.id})
        if not self.request.GET:
            del_url = base_url
        else:
            del_url_dict = QueryDict(mutable=True)
            del_url_dict['_addi'] = self.request.GET.urlencode()
            del_url = '%s?%s' % (base_url, del_url_dict.urlencode())

        return mark_safe("<a href='%s'>删除</a>" % del_url)

    list_display = [
        'note',
        'date',
        display_edit,
        display_del
    ]

    def get_urls(self):
        patterns = [
            re_path('list/(?P<cid>\d+)/$', self.wrapper(self.list_view), name=self.get_list_url_name),
            re_path('add/(?P<cid>\d+)/$', self.wrapper(self.add_view), name=self.get_add_url_name),
            re_path('edit/(?P<cid>\d+)/(?P<nid>\d+)/$', self.wrapper(self.edit_view), name=self.get_edit_url_name),
            re_path('delete/(?P<cid>\d+)/(?P<nid>\d+)/$', self.wrapper(self.delete_view), name=self.get_delete_url_name)
        ]

        patterns.extend(self.extra_urls())

        return patterns

    def get_query_set(self, request, *args, **kwargs):
        current_c_id = kwargs.get('cid')
        current_user_id = request.session['user_info']['id']
        q_set = self.model_class.objects.filter(customer_id=current_c_id, customer__consultant_id=current_user_id)
        return q_set

    def save(self, request, form_obj, is_update, *args, **kwargs):
        customer_id = kwargs.get('cid')
        current_user_id = request.session['user_info']['id']
        obj_exit = models.Customer.objects.filter(id=customer_id, consultant_id=current_user_id).exists()

        if not obj_exit:
            return

        if not is_update:
            form_obj.instance.customer_id = customer_id
            form_obj.instance.consultant_id = current_user_id
        form_obj.save()

    def get_edit_obj(self, request, nid, *args, **kwargs):
        current_c_id = kwargs.get('cid')
        current_user_id = request.session['user_info']['id']
        obj = self.model_class.objects.filter(id=nid, consultant_id=current_user_id, customer_id=current_c_id).first()

        return obj

    def delete_obj(self, request, nid, *args, **kwargs):
        current_c_id = kwargs.get('cid')
        current_user_id = request.session['user_info']['id']
        self.model_class.objects.filter(id=nid, consultant_id=current_user_id, customer_id=current_c_id).delete()
